import pandas as pd
import random

# 手动输入学生数据
def input_student():
    student = {}
    student['姓名'] = input('请输入学生姓名: ')
    student['性别'] = input('请输入学生性别(男/女): ')
    student['班级'] = input('请输入学生班级: ')
    
    # 输入16道题的得分
    for q in range(1, 17):
        while True:
            try:
                score = int(input(f'请输入第{q}题得分(1-5分): '))
                if 1 <= score <= 5:
                    # 反向计分题: 3,6,10,12,15
                    if q in [3, 6, 10, 12, 15]:
                        score = 6 - score  # 5→1, 4→2, 3→3, 2→4, 1→5
                    student[f'题{q}'] = score
                    break
                else:
                    print('请输入1-5之间的整数')
            except ValueError:
                print('请输入有效的整数')
    
    return student

# 生成随机学生数据
def generate_students(num=50):
    names = ['张'+str(i) for i in range(1, num+1)] + ['李'+str(i) for i in range(1, num+1)]
    genders = ['男', '女']
    classes = ['一班', '二班', '三班', '四班']
    
    students = []
    for i in range(num):
        student = {
            '姓名': random.choice(names),
            '性别': random.choice(genders),
            '班级': random.choice(classes)
        }
        
        # 生成16道题的分数(1-5分)
        for q in range(1, 17):
            score = random.randint(1, 5)
            # 反向计分题: 3,6,10,12,15
            if q in [3, 6, 10, 12, 15]:
                score = 6 - score  # 5→1, 4→2, 3→3, 2→4, 1→5
            student[f'题{q}'] = score
        
        students.append(student)
    
    return pd.DataFrame(students)

# 选择数据输入方式
while True:
    choice = input('请选择输入方式:\n1. 手动输入学生数据\n2. 自动生成50名学生数据\n请输入选项(1或2): ')
    if choice == '1':
        students = []
        while True:
            students.append(input_student())
            more = input('是否继续输入?(y/n): ').lower()
            if more != 'y':
                break
        df = pd.DataFrame(students)
        break
    elif choice == '2':
        df = generate_students(50)
        break
    else:
        print('请输入有效的选项(1或2)')

# 计算总分
df['总分'] = df[[f'题{i}' for i in range(1, 17)]].sum(axis=1)

# 根据总分添加提示
df['提示'] = df['总分'].apply(lambda x: 
    '心理素质待加强，需重点关注' if x <= 48 else
    '中等水平，有提升空间' if 49 <= x <= 64 else
    '心理素质良好'
)

# 保存到CSV
csv_path = '/Users/yangyang/Desktop/tools/student_scores.csv'
df.to_csv(csv_path, index=False, encoding='utf-8-sig')
print(f'学生成绩表已生成并保存到: {csv_path}')